package ru.ifmo.ipimenov.ewf_svn.standalone.controller;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.*;

import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Model;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.list.Item;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class DocumentListSelectionListener implements ListSelectionListener {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(DocumentListSelectionListener.class);

    private JLabel labelUp;

    private JList list;

    public DocumentListSelectionListener(JLabel labelUp, JList list){
        this.labelUp = labelUp;
        this.list = list;
    }

    public void valueChanged(ListSelectionEvent e) {
        int index = list.getSelectedIndex();
        if (index < 0){
            index = 1;
            list.setSelectedIndex(1);
        }
        Model m = (Model) list.getModel();
        Item i = m.getItem(index);
        labelUp.setText(String.format("<html><b>id %s </b><span style=\"weight: bold;\">Document</span>&nbsp;&#8594;&nbsp;%s<br>" +
                "&nbsp;&nbsp;<span style=\"weight: bold;\">state</span>&nbsp;&#8594;&nbsp;%s<br>" +
                "&nbsp;&nbsp;<span style=\"weight: bold;\">category</span>&nbsp;&#8594;&nbsp;%s<html>",
                i.getD().getId(),
                i.getD().getName(),
                i.getD().getState().getName(),
                i.getD().getCategory().getName()));
    }
}
